﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IlianaRuler.CPL;
using IlianaRuler.Utils;

namespace IlianaRuler.DAL
{
    /// <summary>
    /// 服务器
    /// </summary>
    struct ServerConfigData
    {
        /// <summary>
        /// 服务器IP
        /// </summary>
        public string ServerIp;
        /// <summary>
        /// 服务器端口
        /// </summary>
        public ushort ServerPort;

        /// <summary>
        /// 客户ID
        /// </summary>
        public string CustomerID;
        /// <summary>
        /// 以MD5加密的登录密码
        /// </summary>
        public byte[] Password;

        /// <summary>
        /// 用于接收客户端指令的本地端口号
        /// </summary>
        public int localPort;
    }

    /// <summary>
    /// 策略设置下发回调类型
    /// </summary>
    /// <param name="nodeType">节点类型</param>
    /// <param name="serverIp">SmartServer IP</param>
    /// <param name="valid">是否有效</param>
    /// <param name="weekDays">策略所对应的一周内哪几天</param>
    /// <param name="rules">规则</param>
    /// <param name="nodes">所针对的节点别名</param>
    delegate void DoSetTactics(byte nodeType, string serverIp,
            bool valid, byte[] weekDays, TacticRule[] rules, string[] nodes);

    /// <summary>
    /// 业务处理类
    /// </summary>
    class Business
    {
        /// <summary>
        /// 获取服务器连接数据
        /// </summary>
        /// <returns></returns>
        public static ServerConfigData GetServerConfig()
        {
            //test code
            ServerConfigData d;
            d.CustomerID = "admin";
            d.Password = Utility.MD5Encrypt(Encoding.ASCII.GetBytes("root"));
            d.ServerIp = "127.0.0.1";
            d.ServerPort = 1299;
            d.localPort = 5112;

            return d;
        }

        /// <summary>
        /// 解析状态指令为控制状态
        /// </summary>
        /// <param name="nodeType"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        public static float ParseState(byte nodeType, string command)
        {
            return 0;
        }

        /// <summary>
        /// 生成一个新的节点别名
        /// </summary>
        /// <param name="nodeType">节点类型</param>
        /// <param name="serverIp">SmartServer IP</param>
        /// <param name="neruonID">节点ID</param>
        /// <returns>生成的节点别名</returns>
        /// <remarks>须保证在同一SmartServer上是唯一的</remarks>
        public static string CreateNodeName(byte nodeType, string serverIp, string neruonID)
        {
            return DateTime.Now.ToString();
        }

        /// <summary>
        /// 以指定的策略ID获取策略设置并下发
        /// </summary>
        /// <param name="tacticId">策略ID</param>
        /// <param name="doSetTactics">下发回调</param>
        /// <remarks>在从数据库获取到指定ID的策略数据后，调用回调方法将策略下发</remarks>
        public static void SetTactics(Guid tacticId, DoSetTactics doSetTactics)
        {

        }

        /// <summary>
        /// 向数据库增加一条新的流水历史记录
        /// </summary>
        /// <param name="id">流水ID</param>
        /// <param name="type">业务类型</param>
        /// <param name="serverIp">操作所针对的SmartServer IP，可能为空</param>
        /// <param name="nodeName">操作所针对的节点别名，可能为空</param>
        public static void RegisterHistory(Guid id, CommandType type, string serverIp, string nodeName)
        {

        }

        /// <summary>
        /// 更新流水历史状态
        /// </summary>
        /// <param name="id">流水ID</param>
        /// <param name="errorCode">错误码</param>
        public static void UpdateHistory(Guid id, ErrorCode errorCode)
        {

        }

        /// <summary>
        /// 登记节点
        /// </summary>
        /// <param name="id">流水ID</param>
        /// <param name="nodeType">节点类型</param>
        /// <param name="serverIp">SmartServer IP</param>
        /// <param name="nodeName">节点别名</param>
        /// <param name="neruonID">节点ID</param>
        /// <param name="bindAlert">是否绑定告警</param>
        /// <param name="errorCode">错误码</param>
        /// <remarks>返回成功时根据设备类型更新相应记录表中的节点别名</remarks>
        public static void RegisterNode(Guid id, byte nodeType, string serverIp,
            string nodeName, string neruonID, bool bindAlert, ErrorCode errorCode)
        {

        }

        /// <summary>
        /// 更换节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nodeType"></param>
        /// <param name="serverIp"></param>
        /// <param name="nodeName"></param>
        /// <param name="neruonId"></param>
        /// <param name="errorCode"></param>
        /// <remarks>返回成功时根据设备类型更新相应记录表中的节点别名</remarks>
        public static void UpdateNode(Guid id, byte nodeType, string serverIp,
            string nodeName, string neruonId, ErrorCode errorCode)
        {

        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nodeType"></param>
        /// <param name="serverIp"></param>
        /// <param name="nodeName"></param>
        /// <param name="errorCode"></param>
        /// <remarks>删除成功后将节点相关数据表中的别名字段置为NULL</remarks>
        public static void RemoveNode(Guid id, byte nodeType, string serverIp,
            string nodeName, ErrorCode errorCode)
        {

        }

        /// <summary>
        /// 节点控制
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nodeType"></param>
        /// <param name="serverIp"></param>
        /// <param name="nodeName"></param>
        /// <param name="state"></param>
        /// <param name="errorCode"></param>
        /// <remarks>不同厂家控制指定的不一致性由表示层负责处理。控制成功后将状态更新至相应数据表的状态字段中。</remarks>
        public static void Control(Guid id, byte nodeType, string serverIp,
            string nodeName, float state, ErrorCode errorCode)
        {

        }

        /// <summary>
        /// 告警处理
        /// </summary>
        /// <param name="id">告警流水ID</param>
        /// <param name="nodeType">节点类型</param>
        /// <param name="serverIp">SmartServer IP</param>
        /// <param name="nodeName">节点别名</param>
        /// <param name="alertType">告警类型</param>
        /// <param name="alertTime">告警时间</param>
        /// <param name="status">节点状态</param>
        public static void Alert(Guid id, byte nodeType, string serverIp, string nodeName, short alertType, DateTime alertTime, NodeStatus status)
        {

        }

        /// <summary>
        /// 获取状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nodeType"></param>
        /// <param name="serverIp"></param>
        /// <param name="nodeName"></param>
        /// <param name="errorCode"></param>
        /// <param name="status"></param>
        /// <remarks>返回成功后将获取到的运行参数记录入数据库相应表中</remarks>
        public static void GetStatus(Guid id, byte nodeType, string serverIp,
            string nodeName, NodeStatus status, ErrorCode errorCode)
        {

        }

        /// <summary>
        /// 自检
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nodeType"></param>
        /// <param name="serverIp"></param>
        /// <param name="nodeName"></param>
        /// <param name="selfCheckingType"></param>
        /// <param name="errorCode"></param>
        /// <remarks>将检查的状态结果写入数据库相应表中</remarks>
        public static void SelfChecking(Guid id, byte nodeType, string serverIp,
            string nodeName, SelfCheckingType selfCheckingType, ErrorCode errorCode,
            int Result)
        {

        }
    }
}
